package com.weirong.smartvault.backservice.entity_model;

import com.weirong.smartvault.backservice.util.common.DateUtil;
import com.weirong.smartvault.backservice.util.common.ListTool;
import com.weirong.smartvault.dao.*;
import com.weirong.smartvault.entity.Command;
import com.weirong.smartvault.entity.Department;
import com.weirong.smartvault.entity.Dict;
import com.weirong.smartvault.entity.ForeignCurrencyRequest;
import com.weirong.smartvault.common.tools.spring.context.SpringContextUtil;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * 于2017/11/26由 KFEB4 创建 .
 */
public class CommandModel {
    private Command command;
    private DictRepository dictRepository;
    private ForeignCurrencyRequestRepository foreignCurrencyRequestRepository;
    private DepartmentRepository departmentRepository ;
    private CommandDetailRepository commandDetailRepository = SpringContextUtil.getBean(CommandDetailRepository.class);
    public CommandModel(Command command){
        this.command = command;
        this.dictRepository = SpringContextUtil.getBean(DictRepository.class);
        this.foreignCurrencyRequestRepository = SpringContextUtil.getBean(ForeignCurrencyRequestRepository.class);
        this.departmentRepository = SpringContextUtil.getBean(DepartmentRepository.class);
    }
    public Integer getId(){
        return command.getId();
    }

    public String getName(){
        return command.getName();
    }
    public String getCreateTime(){
        return DateUtil.getNomalString(command.getCreatedTime());
    }
    public String getState(){
        Dict dict = dictRepository.findDictByCode(command.getState());
        return dict.getName();
    }
    public String getSendResult(){
        return command.getState();
    }
    public String getSendedTime(){
        return DateUtil.getNomalString(command.getCompleteTime());
    };
    public String getDepartmentName() {
        Integer requestId = command.getRequestId();
        if (requestId==null)
            return "无";
        ForeignCurrencyRequest foreignCurrencyRequest =  foreignCurrencyRequestRepository.getOne(requestId);

        Integer BranchId = foreignCurrencyRequest.getBranchId();
        Department department = departmentRepository.findOne(BranchId);
        return department.getName();
    }

    public List<MoneyLogModel> getCommandsDetail() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        return ListTool.cast(commandDetailRepository.findAllByCommand(this.command),MoneyLogModel.class);
    }
}
